package org.hackystat.devcathlon.ui.page.scoreboard.teamleftpanel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;

/**
 * Display a team list with user scores from a chosen Match.
 * 
 * @author Daniel Arakaki, Anthony Du
 */
public class TeamLeftPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Display a list of team members from a chosen match.
   * 
   * @param id Wicket id from the html file.
   * @param matchName A clicked match from the matches scoreboard.
   */
  public TeamLeftPanel(String id, final String matchName) {
    super(id);
    Integer point;
    
    List<Team> teamList = 
      new ArrayList<Team>(MatchManager.getInstance().getMatch(matchName).getTeams());
    
    Iterator<Team> teamNames = teamList.iterator();
    String teamOne = teamNames.next().getName();

    List<User> userList = 
      new ArrayList<User>(TeamManager.getInstance().getTeam(teamOne).getMembers());
    
    final Map<User, Integer> scoreMap = new HashMap<User, Integer>();
    
    //userList.add(TeamManager.getInstance().getTeam(teamOne).getOwner());
    add(new Label("teamOneHeader", TeamManager.getInstance().getTeam(teamOne).getName()));
    
    for (User userData : userList) {
       point = MatchManager.getInstance().getMatch(matchName).getUserPoints(userData);
       scoreMap.put(userData, point);
    }
    
    DataView dataView = new DataView("MemberList", new ListDataProvider(userList)) {

      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        User userItem = (User) item.getModelObject();
        String userName = userItem.getFirstName().concat(" " + userItem.getLastName());
        final String userEmail = userItem.getEmail();

        Link userProfileLink = new Link("userProfileLink", item.getModel()) {

          /** Support serialization. */
          private static final long serialVersionUID = 1L;

          @Override
          public void onClick() {
            DevcathlonSession.get().getProfileSession().setProfile(userEmail);
            setResponsePage(new ProfilePage());
          }
          
        };
        userProfileLink.add(new Label("teamleftpanel", userName));
        item.add(userProfileLink);
        item.add(new Label("playerpoints", scoreMap.get(userItem).toString()));
      }
      
    };
    add(dataView);
  }  
}
